package com.tianji.learning.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tianji.common.exceptions.BadRequestException;
import com.tianji.common.utils.BeanUtils;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.QuestionFormDTO;
import com.tianji.learning.domain.po.InteractionQuestion;
import com.tianji.learning.domain.po.InteractionReply;
import com.tianji.learning.mapper.InteractionQuestionMapper;
import com.tianji.learning.service.IInteractionQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.learning.service.IInteractionReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 互动提问的问题表 服务实现类
 * </p>
 *
 * @author yangyu
 * @since 2023-07-27
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class InteractionQuestionServiceImpl extends ServiceImpl<InteractionQuestionMapper, InteractionQuestion> implements IInteractionQuestionService {

    private final IInteractionReplyService replyService;


    /**
     * 新增互动问题
     *
     * @param questionFormDTO 互动问题表单信息
     */
    @Override
    public void saveQuestion(QuestionFormDTO questionFormDTO) {
        //1.获取登录用户id
        Long userId = UserContext.getUser();
        LocalDateTime now = LocalDateTime.now();

        //2.属性拷贝
        InteractionQuestion question = BeanUtils.copyBean(questionFormDTO, InteractionQuestion.class);

        //3.补充数据
        question.setUserId(userId); //提问学员id
        question.setCreateTime(now); //问题创建时间
        question.setUpdateTime(now); //问题修改时间

        // 4.保存问题
        save(question);
    }


    /**
     * 修改提问
     * @param id 问题id
     * @param questionFormDTO 互动问题表单信息
     */
    @Override
    public void updateQuestion(Long id, QuestionFormDTO questionFormDTO) {
        // 1.获取当前登录用户
        Long userId = UserContext.getUser();

        // 2.查询当前问题
        InteractionQuestion oldQuestion = getById(id);
        if (oldQuestion == null) {
            throw new BadRequestException("问题不存在");
        }

        //3.判断是否是当前用户的问题
        if (!userId.equals(oldQuestion.getUserId())){
            // 不是，抛出异常
            throw new BadRequestException("无权修改他人的问题");
        }

        //4.修改问题
        InteractionQuestion question = BeanUtils.copyBean(questionFormDTO, InteractionQuestion.class);
        question.setUpdateTime(LocalDateTime.now());
        question.setId(id);
        updateById(question);
    }


    /**
     * 根据id删除当前用户问题
     * @param id 问题id
     */
    @Override
    public void deleteById(Long id) {
        // 1.获取当前登录用户
        Long userId = UserContext.getUser();

        // 2.查询当前问题
        InteractionQuestion oldQuestion = getById(id);
        if (BeanUtils.isEmpty(oldQuestion)) {
            throw new BadRequestException("问题不存在");
        }

        //3.判断当前问题是否存在
        if (BeanUtils.isEmpty(oldQuestion)) {
            throw new BadRequestException("问题不存在");
        }

        //4.判断是否是当前用户的问题
        if (!userId.equals(oldQuestion.getUserId())){
            // 不是，抛出异常
            throw new BadRequestException("无权修改他人的问题");
        }


        //5.根据id删除当前用户问题
        deleteById(id);

        //6.删除问题下的回答及评论
        replyService.remove(Wrappers.<InteractionReply>lambdaQuery()
                .eq(InteractionReply::getQuestionId,id));
    }
}
